package com.qf.factory;

import com.qf.dao.UserDao;

import java.io.InputStream;
import java.util.Properties;

/**
 * 自定义工厂  :  一劳永逸  Spring框架写好了
 *
 * @author lixu
 */
public class MyFactory {

    private Properties properties = new Properties();//bean.properties文件中内容

    //String config = bean.properties文件
    public MyFactory(String config) throws Exception{
        //字节流： 输入流   读取配置文件
        InputStream in = MyFactory.class.getResourceAsStream(config);
        properties.load(in);
    }

    //获取对象
    public Object getBean(String name) throws Exception{
        //实例化 com.qf.dao.UserDAOImpl
        String value = properties.getProperty(name);
        //对象的Class  类：
        Class<?> aClass = Class.forName(value);
        return aClass.newInstance();
    }

    //测试
    public static void main(String[] args) throws Exception{

        //1:创建MyFactory 对象   相对路径
        MyFactory myFactory = new MyFactory("/bean.properties");

        UserDao userDao = (UserDao) myFactory.getBean("userDao");
        userDao.findUser();

        //UserService userService = myFactory.getBean("userService");
    }




}
